package model;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import settings.PathSettings;

public class RegistrationValidationBehaviorFactory {

	private static RegistrationValidationBehaviorFactory instance = new RegistrationValidationBehaviorFactory();

	private RegistrationValidationBehaviorFactory() {
	}

	public static RegistrationValidationBehaviorFactory instance() {
		return instance;
	}

	/**
	 * Decorate a registration with RegistrationValidationBehavior as configured in the model.RegistrationValidationBehaviorFactory.properties file
	 * @param registration the Registration to decorate.
	 * @return
	 */
	public RegistrationValidationBehavior getRegistrationValidationBehavior(Registration registration) {

		// Read properties file.
		Properties properties = new Properties();
		try {
			properties.load(new FileInputStream(PathSettings.REGISTRATION_VALIDATION_BEHAVIOR_FACTORY_PROPERTIES));
		} catch (IOException e) {
			e.printStackTrace();
		}
		RegistrationValidationBehavior rgvb = registration;
		Set<Entry<Object, Object>> propertySet = properties.entrySet();
		for (Entry<Object, Object> entry : propertySet) {
			try {
				Class<RegistrationValidator> rgvbClass = (Class<RegistrationValidator>) Class.forName(entry.getValue().toString());
				Constructor<RegistrationValidator> c = rgvbClass.getConstructor(RegistrationValidationBehavior.class);
				rgvb= (RegistrationValidator)c.newInstance(rgvb);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return rgvb;
	}

}
